package com.clear.honghefinance.admin.config;

import com.clear.honghefinance.biz.modules.system.member.domain.vo.AdminDTO;
import com.clear.honghefinance.common.service.AuthFilterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 配置认证过滤器，用于拦截请求并进行身份验证
 */
@Configuration
@Slf4j
public class AuthFilterConfig extends OncePerRequestFilter {

    /**
     * 注入认证过滤服务，用于具体的身份验证逻辑
     */
    @Resource
    private AuthFilterService<AdminDTO> authFilterService;

    /**
     * 执行过滤逻辑
     *
     * @param httpServletRequest  请求对象，包含请求的相关信息
     * @param httpServletResponse 响应对象，用于向客户端发送响应
     * @param filterChain         过滤器链对象，用于继续执行后续过滤器或目标资源
     * @throws ServletException 如果过滤器抛出Servlet异常
     * @throws IOException      如果过滤器抛出IO异常
     */
    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        authFilterService.doFilterInternal(httpServletRequest, httpServletResponse, filterChain);
    }

    /**
     * 决定是否不执行过滤
     *
     * @param request 请求对象，用于判断是否需要过滤
     * @return 如果返回true，则表示不应该过滤该请求；如果返回false，则表示应该过滤
     * @throws ServletException 如果判断过程中抛出Servlet异常
     */
    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
        return authFilterService.shouldNotFilter(request);
    }
}